<?php
namespace Hilaoyu\LaravelExtend;



use Hilaoyu\LaravelExtend\Services\Auth\AuthService;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Hilaoyu\Utils\UtilSupport;


abstract class ControllerBase extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    protected $_menu_actives = [];
    public function __construct()
    {
        $args = func_get_args();
        UtilSupport::tryQuiet(function () use ($args){
            parent::__construct(...$args);
        });

        $this->authenticatedUser();

    }


    public function getPager($default_offset=0){
        $pager_offset = (int)request()->header('pagerOffset',request()->input('pager_offset',$default_offset));
        $pager_start = (int)request()->header('pagerStart',request()->input('pager_start',0));
        $pager_page = request()->header('pagerPage',request()->input('pager_page',null));

        $pager_offset = min(1000,$pager_offset);
        if(!is_null($pager_page)){
            $pager_page = intval($pager_page);
            if ($pager_page < 1) {
                $pager_page = 1;
            }
            $pager_start = ($pager_page - 1) * $pager_offset;
        }

        return [
            'offset' => $pager_offset,
            'start' => $pager_start,
            'page' => $pager_page,
            'total' => 0,
        ];
    }

    protected function renderingResponse(string $view=''){
        if(empty($view)){
            $view = request()->route('_view');

        }
        $view = trim($view);

        if(!$view){
            if(request()->expectsJson()){
                return comResponse()->json();
            }
            return comResponse()->back();
        }

        if('_json_' === $view){
            return comResponse()->json();
        }



        return comResponse()->view($view);
    }


    protected function authenticatedUser($key='',$guards=''){
        return AuthService::AuthenticatedUser($key,$guards);
    }

    /**
     * @return \Illuminate\Foundation\Http\Kernel
     */
    protected function httpKernel(){
        return app('Illuminate\Contracts\Http\Kernel');
    }
}
